```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Docker网络模式详解 | 技术小馆</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .code-block {
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 0.5rem;
            font-family: 'Courier New', Courier, monospace;
        }
        .section-divider {
            border: none;
            height: 1px;
            background: linear-gradient(90deg, transparent, rgba(0,0,0,0.1), transparent);
        }
        .network-icon {
            width: 80px;
            height: 80px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            margin: 0 auto;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <header class="hero-gradient text-white py-20 px-4">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-4">Docker网络模式解析</h1>
                <p class="text-xl md:text-2xl max-w-3xl mb-8">深入理解Docker的三种网络模式及其应用场景</p>
                <div class="flex space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">Bridge模式</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">Host模式</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">None模式</span>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="container mx-auto max-w-6xl px-4 py-12">
        <!-- Introduction Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8 mb-8">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 border-b pb-2">Docker网络模式概述</h2>
                <p class="text-gray-700 mb-6">Docker默认提供了3种网络模式，生成容器时不指定网络模式下默认使用<strong>bridge桥接</strong>模式。</p>
                <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1657964747248-6dd9eb35-5ab4-4adc-9afd-c0149a516296.png" alt="Docker网络模式" class="w-full rounded-lg shadow-md mb-6">
            </div>
        </section>

        <!-- Network Modes Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center text-gray-800">Docker网络模式详解</h2>
            
            <!-- Bridge Mode -->
            <div class="grid md:grid-cols-5 gap-8 mb-12">
                <div class="md:col-span-2 bg-white rounded-xl shadow-lg p-8 card-hover transition-all duration-300">
                    <div class="network-icon bg-blue-100 text-blue-600 mb-6">
                        <i class="fas fa-network-wired text-3xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-4 text-blue-600">Bridge模式</h3>
                    <p class="text-gray-700 mb-4">bridge模式是docker的默认网络模式，不写–net参数，就是bridge模式。使用docker run -p时，docker实际是在iptables做了DNAT规则，实现端口转发功能。</p>
                    <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1657965525922-7c408662-2b1f-412e-8513-873bda98e6b0.png" alt="Bridge模式示意图" class="w-full rounded-lg shadow-md">
                </div>
                <div class="md:col-span-3 bg-white rounded-xl shadow-lg p-8 card-hover transition-all duration-300">
                    <h4 class="text-lg font-semibold mb-4 text-gray-800">工作原理</h4>
                    <p class="text-gray-700 mb-4">当Docker进程启动时，会在主机上创建一个名为docker0的虚拟网桥，此主机上启动的Docker容器都会连接到这个虚拟网桥上。虚拟网桥的工作方式和物理交换机类似，这样主机上的所有容器就通过交换机连在了一个二层网络中。</p>
                    <p class="text-gray-700">从docker0子网中分配一个IP给容器使用，并设置docker0的IP地址为容器的默认网关。在主机上创建一对虚拟网卡veth pair设备，Docker将veth pair设备的一端放在新创建的容器中，并命名为eth0（容器的网卡），另一端放在主机中，以vethxxx这样类似的名字命名，并将这个网络设备加入到docker0网桥中</p>
                </div>
            </div>

            <!-- Host Mode -->
            <div class="grid md:grid-cols-5 gap-8 mb-12">
                <div class="md:col-span-2 bg-white rounded-xl shadow-lg p-8 card-hover transition-all duration-300">
                    <div class="network-icon bg-purple-100 text-purple-600 mb-6">
                        <i class="fas fa-server text-3xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-4 text-purple-600">Host模式</h3>
                    <p class="text-gray-700 mb-4">如果启动容器时使用host模式，那么这个容器将不会获得一个独立的Network Namespace，而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡，配置自己的IP等，而是使用宿主机的IP和端口。</p>
                    <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1657967040304-1d39c9b0-80f3-4771-8a01-87311d055e5d.png" alt="Host模式示意图" class="w-full rounded-lg shadow-md">
                </div>
                <div class="md:col-span-3 bg-white rounded-xl shadow-lg p-8 card-hover transition-all duration-300">
                    <h4 class="text-lg font-semibold mb-4 text-gray-800">特点与限制</h4>
                    <p class="text-gray-700 mb-4">使用host模式的容器可以直接使用宿主机的IP地址与外界通信，容器内部的服务端口也可以使用宿主机的端口，host最大的优势就是网络性能比较好，但是docker host上已经使用的端口就不能再用了，网络的隔离性不好。</p>
                    <div class="code-block p-4 rounded-lg mb-4">
                        <code class="text-white">docker run -it -d --net=host --name t1 tomcat:8.5.81-jre8</code>
                    </div>
                    <p class="text-gray-700">这种模式容器内部不会自动创建虚拟网卡，ip和端口都会使用宿主机的，所以tomcat默认端口号8080是占用虚拟机的网卡。</p>
                </div>
            </div>

            <!-- None Mode -->
            <div class="grid md:grid-cols-5 gap-8 mb-12">
                <div class="md:col-span-2 bg-white rounded-xl shadow-lg p-8 card-hover transition-all duration-300">
                    <div class="network-icon bg-gray-100 text-gray-600 mb-6">
                        <i class="fas fa-ban text-3xl"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-4 text-gray-600">None模式</h3>
                    <p class="text-gray-700 mb-4">使用none模式，Docker容器拥有自己的Network Namespace，但是，并不为Docker容器进行任何网络配置。也就是说，这个Docker容器没有网卡、IP、路由等信息。</p>
                </div>
                <div class="md:col-span-3 bg-white rounded-xl shadow-lg p-8 card-hover transition-all duration-300">
                    <h4 class="text-lg font-semibold mb-4 text-gray-800">安全性与限制</h4>
                    <p class="text-gray-700">需要我们自己为Docker容器添加网卡、配置IP等。这种网络模式下容器只有lo回环网络，没有其他网卡。none模式可以在容器创建时通过–network=none来指定。这种类型的网络没有办法联网，封闭的网络能很好的保证容器的安全性。</p>
                </div>
            </div>
        </section>

        <!-- Practical Example Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 border-b pb-2">实际操作示例</h2>
                
                <!-- Bridge Example -->
                <div class="mb-10">
                    <h3 class="text-xl font-semibold mb-4 text-blue-600">Bridge模式实战</h3>
                    <p class="text-gray-700 mb-4">下面使用bridge模式来启动一个容器，容器内部只有一个tomcat。</p>
                    <div class="code-block p-4 rounded-lg mb-4">
                        <code class="text-white">docker run -it -d -p 8081:8080 --name t2 tomcat:8.5.81-jre8</code>
                    </div>
                    <p class="text-gray-700 mb-4">这种模式下容器内部会自动创建一个虚拟网卡占用8080端口，然后映射到宿主机网卡的端口号是8081。</p>
                    <div class="grid md:grid-cols-2 gap-4">
                        <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1657965996088-3472a839-a1a0-4f89-9bde-68fa6a42278d.png" alt="Bridge模式示例1" class="w-full rounded-lg shadow-md">
                        <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1657966933715-61d7ea18-7953-4aa3-821c-7234e7dca6a9.png" alt="Bridge模式示例2" class="w-full rounded-lg shadow-md">
                    </div>
                </div>
                
                <!-- Host Example -->
                <div class="mb-10">
                    <h3 class="text-xl font-semibold mb-4 text-purple-600">Host模式实战</h3>
                    <div class="code-block p-4 rounded-lg mb-4">
                        <code class="text-white">docker run -it -d --net=host --name t1 tomcat:8.5.81-jre8</code>
                    </div>
                    <div class="grid md:grid-cols-2 gap-4">
                        <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1657967026275-a61bf195-8b59-47a2-ae34-59041dcaba37.png" alt="Host模式示例1" class="w-full rounded-lg shadow-md">
                        <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1657967040304-1d39c9b0-80f3-4771-8a01-87311d055e5d.png" alt="Host模式示例2" class="w-full rounded-lg shadow-md">
                    </div>
                </div>
            </div>
        </section>

        <!-- Redis Sentinel Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 border-b pb-2">Docker搭建Redis主从+哨兵模式</h2>
                <p class="text-gray-700 mb-6">本次搭建Redis集群采用3台Redis，3台哨兵的模式，哨兵负责监控Master和选举工作，Master宕机后需要选举，所以各个容器之间需要通讯，而且还要在项目中连接Redis集群，所以本次集群搭建的网络通讯全部设置为host，这样就不会存在MasterIP变动后访问不了的情况了。所以以下三个容器的IP都是虚拟机的IP，通过端口号来区分Redis实例。</p>
                
                <h3 class="text-xl font-semibold mb-4 text-red-600">Docker-compose.yml配置</h3>
                <div class="code-block p-4 rounded-lg mb-6">
                    <pre class="text-white overflow-x-auto"><code>version: "3.1"
services:
  redis1:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis1
    environment:
      - TZ=Asia/Shanghai
    network_mode: host # 端口号直接使用宿主机端口，所以就不需要在映射了
    volumes:
      - ./conf/redis1.conf:/usr/local/redis/redis.conf
      - ./conf/sentinel1.conf:/data/sentinel.conf        
    command: # 覆盖容器启动的默认命令
      - /bin/sh
      - -c
      - |
            redis-sentinel /data/sentinel.conf
            redis-server /usr/local/redis/redis.conf
  redis2:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis2
    environment:
      - TZ=Asia/Shanghai
    network_mode: host
    volumes:
      - ./conf/redis2.conf:/usr/local/redis/redis.conf
      - ./conf/sentinel2.conf:/data/sentinel.conf        
    command:
      - /bin/sh
      - -c
      - |
            redis-sentinel /data/sentinel.conf
            redis-server /usr/local/redis/redis.conf
  redis3:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis3
    environment:
      - TZ=Asia/Shanghai
    network_mode: host
    volumes:
      - ./conf/redis3.conf:/usr/local/redis/redis.conf
      - ./conf/sentinel3.conf:/data/sentinel.conf        
    command:
      - /bin/sh
      - -c
      - |
            redis-sentinel /data/sentinel.conf
            redis-server /usr/local/redis/redis.conf</code></pre>
                </div>
                
                <h3 class="text-xl font-semibold mb-4 text-red-600">配置文件</h3>
                <div class="grid md:grid-cols-2 gap-4 mb-6">
                    <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1657969330736-691099ce-fc53-47c5-8581-cfe64b67ecd1.png" alt="Redis配置文件1" class="w-full rounded-lg shadow-md">
                    <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1657969344768-f1ba9ea3-b00b-4ab8-abac-954f3096afe4.png" alt="Redis配置文件2" class="w-full rounded-lg shadow-md">
                </div>
                
                <div class="grid md:grid-cols-3 gap-4 mb-6">
                    <div class="code-block p-4 rounded-lg">
                        <h4 class="text-white font-semibold mb-2">redis1.conf</h4>
                        <pre class="text-white"><code>port 6379</code></pre>
                    </div>
                    <div class="code-block p-4 rounded-lg">
                        <h4 class="text-white font-semibold mb-2">redis2.conf</h4>
                        <pre class="text-white"><code>port 6380
replicaof 192.168.147.130 6381 # 因为容器网络做了host，这里直接写虚拟机ip</code></pre>
                    </div>
                    <div class="code-block p-4 rounded-lg">
                        <h4 class="text-white font-semibold mb-2">redis3.conf</h4>
                        <pre class="text-white"><code>port 6381
replicaof 192.168.147.130 6381</code></pre>
                    </div>
                </div>
                
                <div class="grid md:grid-cols-3 gap-4 mb-6">
                    <div class="code-block p-4 rounded-lg">
                        <h4 class="text-white font-semibold mb-2">sentinel1.conf</h4>
                        <pre class="text-white"><code>daemonize yes  #哨兵后台启动
protected-mode no # 关闭保护模式
port 26379 # 哨兵端口
sentinel monitor mymaster 192.168.147.130 6379 2 # 哨兵监控Master的ip
sentinel down-after-milliseconds mymaster 1000 # 心跳时间，超过这个时间就认为Master宕机了</code></pre>
                    </div>
                    <div class="code-block p-4 rounded-lg">
                        <h4 class="text-white font-semibold mb-2">sentinel2.conf</h4>
                        <pre class="text-white"><code>daemonize yes 
protected-mode no 
port 26380 
sentinel monitor mymaster 192.168.147.130 6379 2 
sentinel down-after-milliseconds mymaster 1000</code></pre>
                    </div>
                    <div class="code-block p-4 rounded-lg">
                        <h4 class="text-white font-semibold mb-2">sentinel3.conf</h4>
                        <pre class="text-white"><code>daemonize yes 
protected-mode no 
port 26381
sentinel monitor mymaster 192.168.147.130 6379 2 
sentinel down-after-milliseconds mymaster 1000</code></pre>
                    </div>
                </div>
                
                <h3 class="text-xl font-semibold mb-4 text-red-600">运行容器</h3>
                <div class="code-block p-4 rounded-lg mb-4">
                    <code class="text-white">docker-compose up -d</code>
                </div>
                
                <h3 class="text-xl font-semibold mb-4 text-red-600">项目连接Redis哨兵模式</h3>
                <h4 class="text-lg font-semibold mb-2 text-gray-800">YML配置</h4>
                <div class="code-block p-4 rounded-lg">
                    <pre class="text-white"><code>spring:
  redis:
    sentinel:
      nodes:
        - 192.168.147.130:26379 # 哨兵集群全部写到这里
        - 192.168.147.130:26380
        - 192.168.147.130:26381
      master: mymaster</code></pre>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col items-center">
                <div class="text-xl font-bold text-white mb-2">技术小馆</div>
                <a href="http://www.yuque.com/jtostring" class="text-gray-400 hover:text-white transition-colors duration-300">http://www.yuque.com/jtostring</a>
                <div class="mt-4 text-sm text-gray-500">© 2023 技术小馆 版权所有</div>
            </div>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```